<?php

if(isset($_GET["function"]) && function_exists($_GET['function'])){
    $_GET["function"]();
}

if($_GET['id'])
{
	//sleep(2);
        //agrego custom_field1 a la consulta para traer el codigo de barra
	$sql = sprintf('SELECT * FROM si_products WHERE id = %d LIMIT 1', $_GET['id']);
	$states = dbQuery($sql);
//	$output = '';
	if($states->rowCount() > 0)
	{	
		$row = $states->fetch();

	//	print_r($row);
	//		$output .= '<input id="state" class="field select two-third addr" value="'.$row['unit_price'].'"/>';
			/*Format with decimal places with precision as defined in config.ini*/
			$output['unit_price'] = siLocal::number_clean($row['unit_price']);
                        $output['unit_price2'] = siLocal::number_clean($row['unit_price2']);
                        $output['unit_price3'] = siLocal::number_clean($row['unit_price3']);
			$output['default_tax_id'] = $row['default_tax_id'];
			$output['default_tax_id_2'] = $row['default_tax_id_2'];
                        $output['custom_field1'] = $row['custom_field1'];
                        $output['unitsByProduct']=$row['unitsByProduct'];
                        if($row['unitsByProduct'] != '' && $row['unitsByProduct'] != NULL){
                            //FRACTIONABLE PRODUCT
                            $sql="SELECT * FROM ".TB_PREFIX."precioxrango WHERE product_id = :product_id AND domain_id = :domain_id ORDER BY rangoHasta ASC";
                            $states =  dbQuery($sql, ':product_id', $row['id'],':domain_id',$auth_session->domain_id);
                            if($states->rowCount() > 0){
                               for($i=0;$rangePrice = $states->fetch();$i++) {
                                   $rows[$i]=$rangePrice;
                               }
                                $output['rangePrices']=$rows;
                            }
                        }
	//		$output .= $_POST['id'];
		
	}
	else
	{
		$output .= '';
	}

	echo json_encode($output);
	
	exit();
} else {

echo "";
}

function getProductByBarCode(){
    global $auth_session;
    $sql = sprintf('SELECT * FROM si_products WHERE custom_field1 = %d LIMIT 1', $_GET['barcode']);
	$states = dbQuery($sql);
        
        if($states->rowCount() > 0)
	{	
		$row = $states->fetch();

	//	print_r($row);
	//		$output .= '<input id="state" class="field select two-third addr" value="'.$row['unit_price'].'"/>';
			/*Format with decimal places with precision as defined in config.ini*/
			$output['unit_price'] = siLocal::number_clean($row['unit_price']);
			$output['default_tax_id'] = $row['default_tax_id'];
			$output['default_tax_id_2'] = $row['default_tax_id_2'];
                        $output['description'] = $row['description'];
                        $output['unitsByProduct']=$row['unitsByProduct'];
	//		$output .= $_POST['id'];
                        if($row['unitsByProduct'] != '' && $row['unitsByProduct'] != NULL){
                            
                            $sql="SELECT * FROM ".TB_PREFIX."precioxrango WHERE product_id = :product_id AND domain_id = :domain_id ORDER BY rangoHasta ASC";
                            $states =  dbQuery($sql, ':product_id', $row['id'],':domain_id',$auth_session->domain_id);
                            if($states->rowCount() > 0){
                               for($i=0;$rangePrice = $states->fetch();$i++) {
                                   $rows[$i]=$rangePrice;
                               }
                                $output['rangePrices']=$rows;
                            }
                        }
	}
	else
	{
		$output .= '';
	}

	echo json_encode($output);
	
	exit();
}
// Perform teh Queries!
//$sql = 'SELECT * FROM si_products';
//$country = mysqlQuery($sql) or die('Query Failed:' . mysql_error());


?>
